package com.kotlin.lib.flow

import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking

fun simple(): Flow<Int> = flow {
    for (i in 1..3) {
        delay(100)
        println("emit：$i")
        emit(i)
    }
}/*.flowOn(Dispatchers.Default)*/

fun sequence(): Sequence<Int> = sequence {
    for (i in 1..3) {
        Thread.sleep(100)
        yield(i)
    }
}

fun test2(): Flow<String> = flow {
    repeat(10) {
        delay(120)
        emit("v=$it")
    }
}

fun main() = runBlocking {
    launch {
        repeat(3) {
            delay(100)
            println("${Thread.currentThread().name}:i'm not blocking $it")
        }
    }

    sequence().forEach {
        println("${Thread.currentThread().name}:receive from flow:$it")
    }

    simple().collect {
        println("${Thread.currentThread().name}:receive from flow:$it")

    }

}
